package org.mebusw.dbc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DBConnInfo {
	public static String DBURL = "jdbc:mysql://localhost:3311/mvnforum?useUnicode=true&characterEncoding=latin";
	public static String DBUSER = "";
	public static String DBPASSWORD = "";


	public static void readFromFile() {
		URL base = DBConnInfo.class.getResource("/");

		try {
			String path = new File(base.getFile(), "../" + "DBConnInfo.xml")
					.getCanonicalPath();
			//path="C:/APPS/Tomcat%206.0/webapps/FlexApp/WEB-INF/DBConnInfo.xml";
			path = path.replace("%20", " ");
			BufferedReader f = new BufferedReader(new FileReader(path));
			DBURL = f.readLine();
			DBUSER = f.readLine();
			DBPASSWORD = f.readLine();
			System.out.println(path);
			System.out.println(DBURL);
			System.out.println(DBUSER);
			System.out.println(DBPASSWORD);
			f.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unused")
	private void readFromXml(String fileName) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(fileName);
			NodeList employees = document.getChildNodes();
			for (int i = 0; i < employees.getLength(); i++) {
				Node employee = employees.item(i);
				NodeList employeeInfo = employee.getChildNodes();
				for (int j = 0; j < employeeInfo.getLength(); j++) {
					Node node = employeeInfo.item(j);
					NodeList employeeMeta = node.getChildNodes();
					for (int k = 0; k < employeeMeta.getLength(); k++) {
						System.out.println(employeeMeta.item(k).getNodeName()
								+ ":" + employeeMeta.item(k).getTextContent());
					}
				}
			}
			System.out.println("finish");
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (ParserConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (SAXException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public static void main(String args[]) {
		DBConnInfo.readFromFile();
	}
}
